package com.lqian.simplestructure.linkedlist;

public class Node {
    int data;

    Node next;

    public Node(int data){
        this.data = data;
    }

    //为节点追回节点
    public Node append(Node node){
        Node currentNode = this;

        //找到最后一个节点
        while(true){
            Node nextNode = currentNode.next;
            if (nextNode == null){
                break;
            }
            //going down ...
            currentNode = nextNode;
        }
        currentNode.next = node;
        return this;
    }

    //插入一个节点做为当前节点的下一个节点（在中间插入一个节点）
    public void after(Node node){
        Node nextNode = this.next;
        this.next = node;
        node.next = nextNode;
    }

    //显示所有节点信息(遍历链表)
    public void show(){
        Node currentNode = this;
        while(true){
            System.out.println(currentNode.data+" ");
            currentNode = currentNode.next;
            if (currentNode == null){
                break;
            }
        }
    }

    //删除下一个节点（删除调用节点的下一个）
    public void removeNext(){
        Node newNext = this.next.next;   //跳过 直接连到后面一个节点上
        this.next = newNext;
    }
    //获取下一个节点
    public Node getNextNode(){
        return this.next;
    }

    //获取节点中的数据
    public int getData(){
        return this.data;
    }
    //判断当前节点是否是最后一个节点
    public boolean isLast(){
        return this.next == null;
    }

    //test
    public static void main(String[] args) {
        Node node = new Node(111).append(new Node(222).append(new Node(333)));
        int data = node.getData();
        System.out.println(data);
        node.show();
        int data1 = node.next.next.data;
        System.out.println(data1);

        node.next.removeNext();
        node.show();


    }
}
